import json from '../pages.json'
import qs from 'qs'
import App from '../App.vue'
import deferPromise from './deferPromise.js'
const { onLaunch } = App

const defer = deferPromise()
App.onLaunch = () => {
  console.log(`helloworld`)
  defer.resolve()
  onLaunch()
}

json.pages.forEach((res) => {
  res.path = '/' + res.path
})
export default function() {
  let application,
    hasOriginRouter = false
  let beforeEachCallback = (to, from, next) => {
    next()
  }
  let afterEachCallback = (to, from) => {}
  const getCurrent = () => {
    const pages = getCurrentPages()
    let page = pages[pages.length - 1]
    let { route } = page
    let item = json.pages.find((res) => res.path == '/' + route)
    return Object.assign({}, page, item)
  }
  return {
    install(app) {
      application = app
      defer.then((res) => {
        let { $router } = app.config.globalProperties
        let originRouter$ = app.config.globalProperties.$router
        if (originRouter$) {
          // 初始化$route修正
          let $route = app.config.globalProperties.$route
          let route = this.findRoute($route)
          $route.name = route.name
          hasOriginRouter = true
          let routes = originRouter$.getRoutes()
          routes.forEach((res) => {
            let route = this.findRoute(res)
            if (route) res.name = route.name
          })
          const opush = originRouter$.push
          originRouter$.push = (to) => {
            let toRoute = this.findRoute(to)
            opush({
              path: toRoute.path,
              query: to.query
            })
          }
          originRouter$.beforeEach((to, from, next) => {
            //let toRoute = this.findRoute(to)
            //to.name = toRoute.name
            //let fromRoute = this.findRoute(from)
            //from.name = fromRoute.name
            beforeEachCallback(to, from, next)
          })
          originRouter$.afterEach(afterEachCallback)
        } else {
          //app.mixin({
          //onShow() {
          ////console.log('onshow', this.$route)
          //},
          //onHide() {}
          //})
          let options = uni.getLaunchOptionsSync()
          let route = this.findRoute(options)
          let $route = Object.assign({}, options, route)
          application.config.globalProperties.$route = $route
          //app.mixin({
          //computed: {
          //$route() {
          //return application.config.globalProperties.$route
          //}
          //}
          //})
          app.config.globalProperties.$router = this
        }
      })
      //setTimeout(() => {
      //let router$ = app.config.globalProperties.$router
      //if (router$) router$.beforeEach(beforeEachCallback)
      //}, 1e3)
    },
    toFullPath(to) {
      const { name, params, query } = to
      let route = this.findRoute(to)
      let { path } = route
      let newParams = Object.assign({}, params, query)
      let newParams_query = qs.stringify(newParams)
      let url = path + '?' + newParams_query
      return url
    },
    push(to) {
      const { name, params, query } = to
      const fromPage = getCurrent()
      let url = this.toFullPath(to)
      beforeEachCallback(to, fromPage, () => {
        application.config.globalProperties.$route = to
        uni.navigateTo({
          url,
          success() {
            fromPage.$vm.onPageLeave && fromPage.$vm.onPageLeave()
            const toPage = getCurrent()
            afterEachCallback(toPage, fromPage)
          },
          fail() {}
        })
      })
    },
    beforeEach(cb) {
      beforeEachCallback = (to, from, next) => {
        cb(to, from, next)
      }
    },
    afterEach(cb) {
      afterEachCallback = (to, from) => {
        cb(to, from)
      }
    },
    findRoute(to) {
      let route
      if (to.name) {
        route = json.pages.find((res) => res.name == to.name)
      }
      if (to.path) {
        to.path = to.path[0] != '/' ? '/' + to.path : to.path
        route = json.pages.find((res) => res.path == to.path)
      }
      if (to.path == '/' && !route) {
        route = json.pages[0]
      }
      return route
    }
  }
}
